import 'package:flutter/material.dart';
import "package:cached_network_image/cached_network_image.dart";

class CustomImage extends StatelessWidget {
  const CustomImage({
    required this.url,
    this.width = 200,
    this.height = 200,
    super.key,
  });

  final String url;

  final double? width;

  final double? height; // Replace with your image URL

  @override
  Widget build(BuildContext context) {
    return Container(
      // decoration: BoxDecoration(borderRadius: BorderRadius.circular(5)),
      // clipBehavior: Clip.antiAlias,
      child: CachedNetworkImage(
        imageUrl: url,
        height: height,
        width: width,
        fit: BoxFit.cover,

        // 图片加载时显示的占位符
        placeholder:
            // (BuildContext context, url) => const Center(
            //   // 默认转圈圈
            //   child: CircularProgressIndicator(
            //     color: Color.fromARGB(255, 60, 199, 72),
            //     strokeWidth: 2,
            //   ),
            // ),
            (BuildContext context, url) => Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5),
                color: Color.fromARGB(255, 240, 240, 240),
              ),
              clipBehavior: Clip.antiAlias,

              // 兔子图片
              child: Image.asset(
                "assets/placeholder.png",
                height: height! / 2,
                width: width! / 2,
                fit: BoxFit.cover,
              ),
            ),

        // 图片加载失败时显示的占位符
        errorWidget:
            (BuildContext context, url, error) => Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Color.fromARGB(255, 240, 240, 240),
              ),
              clipBehavior: Clip.antiAlias,
              // 兔子图片
              child: Text("加载失败", style: TextStyle(color: Colors.red)),
            ),
      ),
    );
  }
}
